﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using Common;
using System.ComponentModel;
using GalaSoft.MvvmLight.Ioc;

namespace ViewModel.StationManager
{
    public class StationUserMapDetailViewModel:ViewModelBase
    {
        public ICommand GetStationDetailCommand { get; set; }

        private User m_User;
        public User User
        {
            get { return this.m_User; }
            set { this.m_User = value; base.RaisePropertyChanged(() => this.User); }
        }

        private List<StationInfo> m_StationInfoList;
        public List<StationInfo> StationInfoList
        {
            get { return this.m_StationInfoList; }
            set { this.m_StationInfoList = value;base.RaisePropertyChanged(() => this.StationInfoList); }
        }

        private IStationServcie m_StationService;

        public StationUserMapDetailViewModel(IStationServcie stationService)
        {
            this.m_StationService = stationService;
            this.GetStationDetailCommand = new RelayCommand<StationInfo>(ShowStationDetailForm);
            this.PropertyChanged += OnPropertyChange;
        }

        public async void OnPropertyChange(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "User")
            {
                try
                {
                    if (this.User != null && !string.IsNullOrEmpty(this.User.UserId))
                    {
                        this.StationInfoList = (await this.m_StationService.QueryStationsAsync(new StationQueryCriteria { UserId = this.User.UserId })).ToList();
                    }
                }
                catch (Exception ex)
                {
                    await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("站点信息加载失败 ", "错误", "确定", null);
                    Log.WriteException(ex);
                }
            }
        }

        public void ShowStationDetailForm(StationInfo stationInfo)
        {
            StationDetailViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<StationDetailViewModel>();
            vm.StationInfo = stationInfo;
            this.MessengerInstance.Send<ViewModelBase>(vm, "StationUserMapDetail_ShowStationDetail");
        }
    }
}
